<?php
namespace app\common\model;

use think\Model;

/**
 * 合同模型
 * Class AdminUser
 * @package app\common\model
 */
class Contract extends Model
{
    /**
     * 查询一条用户信息
     *
     * @param array $params
     * @param bool $fields
     * @param bool $need_array
     * @return array
     */
    public function getInfo(array $params, $fields = true, $need_array = false)
    {
        $where = $return = [];

        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['contract_number']) && !empty($params['contract_number'])) {
            $where['contract_number'] = $params['contract_number'];
        }

        if (isset($params['start']) && !empty($params['end'])) {
            $where['take_back_operate_time'] = ['between', [$params['start'], $params['end']]];
        }

        if (!empty($where)) {
            if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
                $where['is_delete'] = $params['is_delete'];
            } else {
                $where['is_delete'] = 0;
            }

            $return = $this->where($where)->field($fields)->find();
            if (!empty($return) && $need_array) {
                $return = $return->toArray();
            }
        }

        return $return;
    }

    /**
     * @param $id
     * @param array $req
     * @return bool
     */
    public function saveDate($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $this->data = [];
        $data = $req;
        $res = $this->save($data, ['id' => $id]);
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $res;
    }

    /**
     * @param array $data
     * @return bool|mixed
     */
    public function addData(array $data)
    {
        $res = $this->allowField(true)->data($data, true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;
        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];
        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @param bool $is_need_arr
     * @return mixed
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true, $is_need_arr = false)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['start']) && !empty($params['end'])) {
            $where['take_back_operate_time'] = ['between', [$params['start'], $params['end']]];
        }

        if (isset($params['project_group_id'])) {
            $where['project_group_id'] = $params['project_group_id'];
        }

        if (isset($params['is_delete']) && is_numeric($params['is_delete'])) {
            $where['is_delete'] = $params['is_delete'];
        } else {
            $where['is_delete'] = 0;
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit, false, ['query' => request()->param()]);

        return $is_need_arr ? $data->toArray() : $data;
    }

    /**
     * 获取该年份中被废弃的同前缀的合同，且合同号未被占用的 合同序号
     * @param $year
     * @param $prefix
     * @return mixed
     */
    public function getbeDiscardContractNumber($year, $prefix)
    {
        $where = [
            'year'              => $year,
            'sign_company_code' => $prefix,
            'status'            => CONTRACT_STATUS_DISCARD,
            'be_used_number'    => 0,
            'is_delete'         => STATUS_NOT_DELETE,
        ];
        $res = $this->field('id,serial_number,contract_number')->where($where)->find();
        return empty($res) ? [] : $res->toArray();
    }

    /**
     * 获取指定年份的最新的序号
     *
     * @param int $year 年份
     * @param string $sign_company_code 签订合同的公司代号
     * @return int
     */
    public function getLastContractNumber($year, $sign_company_code)
    {
        $where = [
            'year'                 => $year,
            'sign_company_code'    => $sign_company_code,
            'is_delete'            => STATUS_NOT_DELETE,
        ];
        $res = $this->field('max(serial_number) as serial_number')->where($where)->find();
        $res = $res->toArray();
        if (empty($res['serial_number'])) {
            return 0;
        } else {
            return $res['serial_number'];
        }
    }
}